package services;

import java.util.List;

import enums.EPState;
import models.Colegio;
import play.cache.Cache;
import util.states.StateLogic;

public class ColegioProcessSrv {

	public static void startProcess(Colegio c) throws InstantiationException,
			IllegalAccessException {

		enterTransition(c, EPState.CONFIG_COLEGIO);

	}

	public static List<String> goNext(Colegio c) throws InstantiationException,
			IllegalAccessException {
		EPState state = c.getState();
		StateLogic sl = state.getStateLogic().newInstance();
		sl.setColegio(c);
		List<String> errors = sl.validateTransition();
		if (sl.validateTransition().isEmpty()) {
			EPState nextState = sl.getNext();
			enterTransition(c, nextState);
		}
		DAO.merge(c);
		Cache.set("idColegio=" + c.getId(), c);
		return errors;
	}

	private static void enterTransition(Colegio c, EPState nextState)
			throws InstantiationException, IllegalAccessException {
		c.setState(nextState);
		StateLogic sl2 = nextState.getStateLogic().newInstance();
		sl2.setColegio(c);
		sl2.enterTransition();

	}

}
